import {
  detail,
  addContractOrder,
  contractOrderDetail,
  updateContractOrder,
  placeOrder,
  acceptDesign,
  denyDesign,
  quoteOrder,
  acceptQuote,
  denyQuote,
  publishOrder,
  deleteContractOrder,
  doDesign,
  publishAgain,
  isPushOms,
  isPushOmsModel,
  complete,
  listAllPayOrder,
  toTenant,
  refreshFromOms,
  cancelPayOrder
} from 'services/order'
import { type, query } from 'services/product'
import { getSystemProperty } from 'services/app'
import pathToRegexp from 'path-to-regexp'
import { Modal } from 'antd'
import moment from 'moment'
import { routerRedux } from 'dva/router'
import { config } from 'utils'
import { getRegionCodes, getReginNames } from 'utils/utils';
import { stat } from 'fs';
import lodash from 'lodash';

const { defaultPageSize } = config

export default {
  namespace: 'updateContractOrder',
  state: {
    currentOrderDetail: {},
    currentSystemProperty: {},
    isShowAddEndContractModal: false,
    isUpdate: false,
    currentContractDetail: {},
    isShowNeedDesignContractModal: false,
    isShowNoDesignContractModal: false,
    isDesignNeeded: false,
    isShowConfirmDesignModal: false,
    isShowQuoteModal: false,
    isShowConfirmQuoteModal: false,
    isShowPublishEndOrderModal: false,
    isShowPublishCustomizeOrderModal: false,
    isShowDoDesignModal: false,
    isShowPublishAgainCustomizeOrderModal: false,
    listState: '',
    isShowContractDetailModal: false,
    confirmAddLoading: false,
    isPushOmsModel: true,
    currentRefreshRecordId: "",
    payOrderList: [],
    isShowPayOrderPushModal: false,
    currentPayOrder: {},
    purchaseType: [],
    materialItems: [],
    commodities: [],
    material: {
      modalVisible: false,
      selectedKeys: [],
      selectedItems: [],
      items: [],
      pagination: {
        current: 1,
        total: 0,
        showTotal: total => `共 ${total} 条`,
        showSizeChanger: true,
        showQuickJumper: true,
        defaultPageSize: defaultPageSize
      },
      pageOptions: {
        sort: 'createTime',
        order: 'DESC'
      },
      queryParam: {},
      typeTree: [],
      treeSearchKeyword: '',
      typeItem: {}
    }
  },
  subscriptions: {
    setup({ dispatch, history }) {
      history.listen((location) => {
        const pathname = location.pathname
        const match = pathToRegexp('/order/updateContractOrder/:id').exec(pathname)
        if (match) {
          let orderId = match[1]
          dispatch({ type: 'resetCurrentOrderDetail' })
          dispatch({ type: 'getCurrentSystemProperty' })
          dispatch({
            type: 'getOrderDetail', payload: {
              orderId
            }
          })
          dispatch({
            type: 'listAllPayOrder', payload: orderId
          })
          dispatch({
            type: 'setIsShowPayOrderPushModal', payload: false
          })
          if (_.isEmpty(location.state)) {
            if (location.state === undefined || location.state.listState === undefined || location.state.listState === null) {
              window.location.href = '/orderList/myOrder'
              return
            } else {
              dispatch({
                type: 'setListState',
                payload: {
                  listState: location.state.listState
                }
              })
            }
          }
        }
      })
    }
  },
  effects: {
    * getOrderDetail({
      payload
    }, { select, call, put }) {
      const data = yield call(detail, payload.orderId)
      // 处理省市区数据
      const orderDetail = data.result;
      orderDetail.consigneeRegionName = getReginNames(orderDetail.consigneeProvince, orderDetail.consigneeCity, orderDetail.consigneeDistrict)
      orderDetail.consigneeRegion = getRegionCodes(orderDetail.consigneeProvince, orderDetail.consigneeCity, orderDetail.consigneeDistrict);
      if (orderDetail.consigneeRegion[0] === null && orderDetail.consigneeRegion[1] === null && orderDetail.consigneeRegion[2] === null) {
        orderDetail.consigneeRegion = null
      }
      yield put({
        type: 'setCurrentOrderDetail',
        payload: {
          currentOrderDetail: data.result
        }
      })
    },
    /**
     * 显示合同订单详情
     */
    * onShowContractDetail({
      payload
    }, { select, call, put }) {
      const data = yield call(contractOrderDetail, payload.salesOrderId, payload.contractOrderId)
      yield put({
        type: 'detailSuccess',
        payload: {
          currentContractDetail: data.result
        }
      })
      yield put({ type: 'showContractDetailModal', payload: {} })
    },
    /**
     * 创建成品合同 before
     */
    * toCreateEndContract({
      payload
    }, { select, call, put }) {
      // 获取当前租户系统配置
      const data = yield call(getSystemProperty)
      yield put({
        type: 'setCurrentSystemProperty',
        payload: {
          currentSystemProperty: data.result
        }
      })
      yield put({ type: 'showAddEndContractModal', payload: {} })
    },
    * getCurrentSystemProperty({
      payload
    }, { select, call, put }) {
      // 获取当前租户系统配置
      const data = yield call(getSystemProperty)
      yield put({
        type: 'setCurrentSystemProperty',
        payload: {
          currentSystemProperty: data.result
        }
      })
    },

    /**
     * 物料类型树
     * @param payload
     * @param select
     * @param call
     * @param put
     * @returns {IterableIterator<*>}
     */
    * getMaterialType({
      payload
    }, { select, call, put }) {
      const res = yield call(type)
      yield put({ type: 'setMaterialTypeTree', payload: res.list })
    },
    * listAllPayOrder({
      payload
    }, { select, call, put }) {
      const res = yield call(listAllPayOrder, payload)
      yield put({ type: 'setPayOrderList', payload: res.result })
    },

    /**
     * 物料列表
     * @param payload
     * @param select
     * @param call
     * @param put
     * @returns {IterableIterator<*>}
     */
    * getMaterialList({
      payload
    }, { select, call, put }) {
      yield put({ type: 'asyncMaterialData', payload: payload })
      const pageOptions = yield select(({ updateContractOrder }) => updateContractOrder.material.pageOptions)
      const queryParam = yield select(({ updateContractOrder }) => updateContractOrder.material.queryParam)
      const typeItem = yield select(({ updateContractOrder }) => updateContractOrder.material.typeItem)

      queryParam.productTypeId = typeItem.id
      const queryPayload = {
        ...pageOptions,
        queryParam: queryParam
      }

      const data = yield call(query, queryPayload)

      yield put({
        type: 'materialQuerySuccess',
        payload: {
          ...data
        }
      })
    },

    /**
     * 新增合同
     */
    * addContractOrder({
      payload
    }, { select, call, put }) {
      console.info('进入新增合同model层')
      const currentOrderDetail = yield select(({ updateContractOrder }) => updateContractOrder.currentOrderDetail)
      console.info('获取当前订单的详情')
      const data = yield call(addContractOrder, currentOrderDetail.id, payload.contractOrder)
      console.info('添加合同成功')
      if (data.code === 0) {
        Modal.success({
          title: '新增合同成功',
          onOk: () => { }
        })
        console.info('Modal框结束')
        yield put({ type: 'hideAddContractModal', payload: {} })
        console.info('隐藏添加modal框并设置loading状态')
        yield put({
          type: 'setAddLoading',
          payload: {
            payload: {
              confirmAddLoading: false
            }
          }
        })
        console.info('刷新页面之前')
        yield put({
          type: 'getOrderDetail',
          payload: {
            orderId: currentOrderDetail.id
          }
        })
        console.info('刷新页面之后')
      } else {
        Modal.error({ title: '新增合同失败！' })
      }
    },
    /**
     * 删除合同
     */
    * deleteContract({
      payload
    }, { select, call, put }) {
      const currentOrderDetail = yield select(({ updateContractOrder }) => updateContractOrder.currentOrderDetail)
      yield call(deleteContractOrder, currentOrderDetail.id, payload.contractOrderId)
      yield put({
        type: 'getOrderDetail',
        payload: {
          orderId: currentOrderDetail.id
        }
      })
      Modal.success({ title: '删除合同成功' })
    },
    /**
     * 编辑合同 before
     */
    * toUpdateContract({
      payload
    }, { select, call, put }) {
      const currentOrderDetail = yield select(({ updateContractOrder }) => updateContractOrder.currentOrderDetail)
      const contractOrder = payload.contractOrder
      const data = yield call(contractOrderDetail, currentOrderDetail.id, contractOrder.id)
      // 处理数据
      data.result.consigneeRegion = getRegionCodes(data.result.consigneeProvince, data.result.consigneeCity, data.result.consigneeDistrict);
      data.result.orderContractAt = moment(data.result.orderContractAt)
      data.result.installContractAt = moment(data.result.installContractAt)
      if (data.result.measuredAt) {
        data.result.measuredAt = moment(data.result.measuredAt)
      }
      data.result.pushToOMSNeeded = data.result.pushToOMSNeeded + ''
      yield put({
        type: 'setCurrentContractDetail',
        payload: {
          currentContractDetail: data.result
        }
      })

      if (contractOrder.contractOrderType === 'END') {
        yield put({
          type: 'showAddEndContractModal',
          payload: {
            isUpdate: true
          }
        })
      }
      if (contractOrder.contractOrderType === 'CUSTOMIZE' && contractOrder.designNeeded) {
        yield put({
          type: 'showNeedDesignContractModal',
          payload: {
            isUpdate: true
          }
        })
      }
      if (contractOrder.contractOrderType === 'CUSTOMIZE' && !contractOrder.designNeeded) {
        yield put({
          type: 'showNoDesignContractModal',
          payload: {
            isUpdate: true
          }
        })
      }
    },
    /**
     * 修改合同 after
     */
    * updateContractOrder({
      payload
    }, { select, call, put }) {
      const currentOrderDetail = yield select(({ updateContractOrder }) => updateContractOrder.currentOrderDetail)
      yield call(updateContractOrder, currentOrderDetail.id, payload.contractOrder.id, payload.contractOrder)
      yield put({
        type: 'hideAddContractModal',
        payload: {
          isUpdate: false,
          currentContractDetail: {}
        }
      })
      yield put({
        type: 'getOrderDetail',
        payload: {
          orderId: currentOrderDetail.id
        }
      })
      Modal.success({ title: '修改合同成功' })
    },
    /**
     * 提交设计方案 before
     */
    * toDoDesign({
      payload
    }, { select, call, put }) {
      const currentOrderDetail = yield select(({ updateContractOrder }) => updateContractOrder.currentOrderDetail)
      const contractOrder = payload.contractOrder
      const data = yield call(contractOrderDetail, currentOrderDetail.id, contractOrder.id)
      // 处理数据
      data.result.orderContractAt = moment(data.result.orderContractAt)
      data.result.installContractAt = moment(data.result.installContractAt)
      if (data.result.measuredAt) {
        data.result.measuredAt = moment(data.result.measuredAt)
      }
      yield put({
        type: 'setCurrentContractDetail',
        payload: {
          currentContractDetail: data.result
        }
      })
      yield put({ type: 'showDoDesignModal', payload: {} })
    },
    /**
     * 提交设计方案 after
     */
    * doDesign({
      payload
    }, { select, call, put }) {
      const currentOrderDetail = yield select(({ updateContractOrder }) => updateContractOrder.currentOrderDetail)
      const currentContractDetail = yield select(({ updateContractOrder }) => updateContractOrder.currentContractDetail)
      yield call(doDesign, currentOrderDetail.id, currentContractDetail.id, payload.doDesignInfo)
      yield put({ type: 'hideDoDesignModal', payload: {} })
      yield put({
        type: 'getOrderDetail',
        payload: {
          orderId: currentOrderDetail.id
        }
      })
      Modal.success({ title: '合同设计成功' })
    },
    /**
     * 合同下单
     */
    * placeOrder({
      payload
    }, { select, call, put }) {
      const currentOrderDetail = yield select(({ updateContractOrder }) => updateContractOrder.currentOrderDetail)
      yield call(placeOrder, currentOrderDetail.id, payload.contractOrderId, payload.version)
      yield put({
        type: 'getOrderDetail',
        payload: {
          orderId: currentOrderDetail.id
        }
      })
      Modal.success({ title: '合同下单成功' })
    },
    /**
     * 确认设计 before
     */
    * toConfirmDesign({
      payload
    }, { select, call, put }) {
      const currentOrderDetail = yield select(({ updateContractOrder }) => updateContractOrder.currentOrderDetail)
      const contractOrder = payload.contractOrder
      const data = yield call(contractOrderDetail, currentOrderDetail.id, contractOrder.id)
      yield put({
        type: 'setCurrentContractDetail',
        payload: {
          currentContractDetail: data.result
        }
      })
      yield put({ type: 'showConfirmDesignModal', payload: {} })
    },
    /**
     * 确认设计
     */
    * acceptDesign({
      payload
    }, { select, call, put }) {
      const currentOrderDetail = yield select(({ updateContractOrder }) => updateContractOrder.currentOrderDetail)
      const currentContractDetail = yield select(({ updateContractOrder }) => updateContractOrder.currentContractDetail)
      yield call(acceptDesign, currentOrderDetail.id, currentContractDetail.id, currentContractDetail.version)
      yield put({ type: 'hideConfirmDesignModal', payload: {} })
      yield put({
        type: 'getOrderDetail',
        payload: {
          orderId: currentOrderDetail.id
        }
      })
      Modal.success({ title: '确认设计成功' })
    },
    /**
     * 设计不通过
     */
    * denyDesign({
      payload
    }, { select, call, put }) {
      const currentOrderDetail = yield select(({ updateContractOrder }) => updateContractOrder.currentOrderDetail)
      const currentContractDetail = yield select(({ updateContractOrder }) => updateContractOrder.currentContractDetail)
      yield call(denyDesign, currentOrderDetail.id, currentContractDetail.id, currentContractDetail.version, payload.denyParams)
      yield put({ type: 'hideConfirmDesignModal', payload: {} })
      yield put({
        type: 'getOrderDetail',
        payload: {
          orderId: currentOrderDetail.id
        }
      })
      Modal.success({ title: '设计不通过成功' })
    },
    /**
     * 报价 before
     */
    * toQuote({
      payload
    }, { select, call, put }) {
      const currentOrderDetail = yield select(({ updateContractOrder }) => updateContractOrder.currentOrderDetail)
      const contractOrder = payload.contractOrder
      const data = yield call(contractOrderDetail, currentOrderDetail.id, contractOrder.id)
      yield put({
        type: 'setCurrentContractDetail',
        payload: {
          currentContractDetail: data.result
        }
      })
      yield put({ type: 'showQuoteModal', payload: {} })
    },
    /**
     * 报价 after
     */
    * quoteOrder({
      payload
    }, { select, call, put }) {
      const currentOrderDetail = yield select(({ updateContractOrder }) => updateContractOrder.currentOrderDetail)
      const currentContractDetail = yield select(({ updateContractOrder }) => updateContractOrder.currentContractDetail)
      yield call(quoteOrder, currentOrderDetail.id, currentContractDetail.id, payload.quoteInfo)
      yield put({ type: 'hideQuoteModal', payload: {} })
      yield put({
        type: 'getOrderDetail',
        payload: {
          orderId: currentOrderDetail.id
        }
      })
      Modal.success({ title: '合同报价成功' })
    },
    /**
     * 确认报价 before
     */
    * toConfirmQuote({
      payload
    }, { select, call, put }) {
      const currentOrderDetail = yield select(({ updateContractOrder }) => updateContractOrder.currentOrderDetail)
      const contractOrder = payload.contractOrder
      const data = yield call(contractOrderDetail, currentOrderDetail.id, contractOrder.id)
      yield put({
        type: 'setCurrentContractDetail',
        payload: {
          currentContractDetail: data.result
        }
      })
      yield put({ type: 'showConfirmQuoteModal', payload: {} })
    },
    /**
     * 确认报价
     */
    * acceptQuote({
      payload
    }, { select, call, put }) {
      const currentOrderDetail = yield select(({ updateContractOrder }) => updateContractOrder.currentOrderDetail)
      const currentContractDetail = yield select(({ updateContractOrder }) => updateContractOrder.currentContractDetail)
      yield call(acceptQuote, currentOrderDetail.id, currentContractDetail.id, currentContractDetail.version)
      yield put({ type: 'hideConfirmQuoteModal', payload: {} })
      yield put({
        type: 'getOrderDetail',
        payload: {
          orderId: currentOrderDetail.id
        }
      })
      Modal.success({ title: '确认报价成功' })
    },
    /**
     * 报价不通过
     */
    * denyQuote({
      payload
    }, { select, call, put }) {
      const currentOrderDetail = yield select(({ updateContractOrder }) => updateContractOrder.currentOrderDetail)
      const currentContractDetail = yield select(({ updateContractOrder }) => updateContractOrder.currentContractDetail)
      yield call(denyQuote, currentOrderDetail.id, currentContractDetail.id, currentContractDetail.version, payload.denyParams)
      yield put({ type: 'hideConfirmQuoteModal', payload: {} })
      yield put({
        type: 'getOrderDetail',
        payload: {
          orderId: currentOrderDetail.id
        }
      })
      Modal.success({ title: '报价不通过成功' })
    },
    /**
     * 下推到OMS before
     */
    * toPublishToOms({
      payload
    }, { select, call, put }) {
      const currentOrderDetail = yield select(({ updateContractOrder }) => updateContractOrder.currentOrderDetail)
      const contractOrder = payload.contractOrder
      const data = yield call(contractOrderDetail, currentOrderDetail.id, contractOrder.id)
      // 处理数据
      data.result.orderContractAt = moment(data.result.orderContractAt)
      data.result.installContractAt = moment(data.result.installContractAt)
      if (!_.isEmpty(data.result.consigneeProvince)) {
        data.result.receiptAddress = getReginNames(data.result.consigneeProvince, data.result.consigneeCity, data.result.consigneeDistrict) + data.result.consigneeAddress
      }
      data.result.receiptUserName = data.result.consigneeName
      data.result.receiptUserPhoneNum = data.result.consigneePhoneNum
      yield put({
        type: 'setCurrentContractDetail',
        payload: {
          currentContractDetail: data.result
        }
      })
      if (contractOrder.contractOrderType === 'CUSTOMIZE') {
        yield put({ type: 'showPublishCustomizeOrderModal', payload: {} })
      }
      if (contractOrder.contractOrderType === 'END') {
        yield put({ type: 'showPublishEndOrderModal', payload: {} })
      }
    },
    /**
     * 推送到OMS
     */
    * publishToOms({
      payload
    }, { select, call, put }) {
      const currentOrderDetail = yield select(({ updateContractOrder }) => updateContractOrder.currentOrderDetail)
      const currentContractDetail = yield select(({ updateContractOrder }) => updateContractOrder.currentContractDetail)
      yield call(publishOrder, currentOrderDetail.id, currentContractDetail.id, payload.publishOrderInfo)
      if (currentContractDetail.contractOrderType === 'CUSTOMIZE') {
        yield put({ type: 'hidePublishCustomizeOrderModal', payload: {} })
      }
      if (currentContractDetail.contractOrderType === 'END') {
        yield put({ type: 'hidePublishEndOrderModal', payload: {} })
      }
      yield put({
        type: 'getOrderDetail',
        payload: {
          orderId: currentOrderDetail.id
        }
      })
      Modal.success({ title: '推送OMS成功' })
    },
    /**
     * 再次下推到OMS before
     */
    * toPublishAgain({
      payload
    }, { select, call, put }) {
      const currentOrderDetail = yield select(({ updateContractOrder }) => updateContractOrder.currentOrderDetail)
      const contractOrder = payload.contractOrder
      const data = yield call(contractOrderDetail, currentOrderDetail.id, contractOrder.id)
      data.result.orderPublishInfoSummary.orderContractAt = moment(data.result.orderPublishInfoSummary.orderContractAt)
      data.result.orderPublishInfoSummary.installContractAt = moment(data.result.orderPublishInfoSummary.installContractAt)
      yield put({
        type: 'setCurrentContractDetail',
        payload: {
          currentContractDetail: data.result
        }
      })
      yield put({ type: 'showPublishAgainCustomizeModal', payload: {} })
    },
    /**
     * 再次推送到OMS
     */
    * publishAgain({
      payload
    }, { select, dispatch, call, put }) {
      const currentOrderDetail = yield select(({ updateContractOrder }) => updateContractOrder.currentOrderDetail)
      const currentContractDetail = yield select(({ updateContractOrder }) => updateContractOrder.currentContractDetail)
      const data = yield call(publishAgain, currentOrderDetail.id, currentContractDetail.id, payload.publishOrderInfo)
      yield put({ type: 'hidePublishAgainCustomizeModal', payload: {} })
      if (data.code === 0) {
        yield put({
          type: 'getOrderDetail',
          payload: {
            orderId: currentOrderDetail.id
          }
        })
      }
      Modal.success({ title: '再次推送OMS成功' })
    },
    * refresh({
      payload
    }, { select, call, put }) {
      yield put(routerRedux.push(`/order/updateContractOrder/` + payload.id))
    },
    * refreshFromOms({
      payload
    }, { select, call, put }) {
      yield call(refreshFromOms, payload.contractNum)
      yield put({
        type: 'getOrderDetail',
        payload: {
          orderId: payload.salesOrderId
        }
      })
    },
    * cancelPayOrder({
      payload
    }, { select, call, put }) {
      const currentOrderDetail = yield select(({ updateContractOrder }) => updateContractOrder.currentOrderDetail)
      yield call(cancelPayOrder, payload)
      yield put({
        type: 'listAllPayOrder', payload: currentOrderDetail.id
      })
    },
    * complete({
      payload
    }, { select, call, put }) {
      const currentOrderDetail = yield select(({ updateContractOrder }) => updateContractOrder.currentOrderDetail)
      yield call(complete, payload)
      yield put({
        type: 'getOrderDetail',
        payload: {
          orderId: currentOrderDetail.id
        }
      })
      yield put({
        type: 'listAllPayOrder', payload: currentOrderDetail.id
      })
      yield put({
        type: 'setIsShowPayOrderPushModal',
        payload: false
      });
    },
    /**
     * 申请变更
     */
    * toChangeOrder({
      payload
    }, { select, call, put }) {
      yield put(routerRedux.push(`/order/orderChangeApply`))
    },
    * isPushOms({
      payload
    }, { select, call, put }) {
      yield put({
        type: 'setNotPushOms',
        payload: {
          isPushOms: payload.isPushOms
        }
      })
    },
    * toTenant({
      payload
    }, { select, call, put }) {
      const currentOrderDetail = yield select(({ updateContractOrder }) => updateContractOrder.currentOrderDetail)
      const currentContractDetail = yield select(({ updateContractOrder }) => updateContractOrder.currentContractDetail)
      yield call(toTenant, currentOrderDetail.id, currentContractDetail.id, payload)
    },
    * goOrderSalesmarket({ item, salesDistributorId }, { select, call, put }) {
      yield put(routerRedux.push('/order/salesmarket', { item: lodash.cloneDeep(item), salesDistributorId: salesDistributorId }))
    }
  },
  reducers: {
    setNotPushOms(state, { payload }) {
      return {
        ...state,
        ...payload,
        isPushOmsModel: payload.isPushOms
      }
    },
    setCurrentOrderDetail(state, { payload }) {
      return {
        ...state,
        ...payload,
        currentOrderDetail: payload.currentOrderDetail
      }
    },
    setCurrentContractDetail(state, { payload }) {
      return {
        ...state,
        ...payload,
        currentContractDetail: payload.currentContractDetail
      }
    },
    showAddEndContractModal(state, { payload }) {
      return {
        ...state,
        ...payload,
        isShowAddEndContractModal: true
      }
    },
    hideAddContractModal(state, { payload }) {
      return {
        ...state,
        ...payload,
        isShowAddEndContractModal: false,
        isShowNeedDesignContractModal: false,
        isShowNoDesignContractModal: false,
        isUpdate: false,
        currentContractDetail: {}
      }
    },
    showNeedDesignContractModal(state, { payload }) {
      return {
        ...state,
        ...payload,
        isShowNeedDesignContractModal: true,
        isDesignNeeded: true
      }
    },
    showNoDesignContractModal(state, { payload }) {
      return {
        ...state,
        ...payload,
        isShowNoDesignContractModal: true,
        isDesignNeeded: false
      }
    },
    showConfirmDesignModal(state, { payload }) {
      return {
        ...state,
        ...payload,
        isShowConfirmDesignModal: true
      }
    },
    hideConfirmDesignModal(state, { payload }) {
      return {
        ...state,
        ...payload,
        isShowConfirmDesignModal: false,
        currentContractDetail: {}
      }
    },
    showQuoteModal(state, { payload }) {
      return {
        ...state,
        ...payload,
        isShowQuoteModal: true
      }
    },
    hideQuoteModal(state, { payload }) {
      return {
        ...state,
        ...payload,
        isShowQuoteModal: false,
        currentContractDetail: {}
      }
    },
    showConfirmQuoteModal(state, { payload }) {
      return {
        ...state,
        ...payload,
        isShowConfirmQuoteModal: true
      }
    },
    hideConfirmQuoteModal(state, { payload }) {
      return {
        ...state,
        ...payload,
        isShowConfirmQuoteModal: false,
        currentContractDetail: {}
      }
    },
    showPublishEndOrderModal(state, { payload }) {
      return {
        ...state,
        ...payload,
        isShowPublishEndOrderModal: true
      }
    },
    hidePublishEndOrderModal(state, { payload }) {
      return {
        ...state,
        ...payload,
        isShowPublishEndOrderModal: false,
        currentContractDetail: {}
      }
    },
    addMaterielItem(state, { payload }) {
      return {
        ...state,
        ...payload,
        currentContractDetail: payload.currentContractDetail
      }
    },
    showPublishCustomizeOrderModal(state, { payload }) {
      return {
        ...state,
        ...payload,
        isShowPublishCustomizeOrderModal: true
      }
    },
    hidePublishCustomizeOrderModal(state, { payload }) {
      return {
        ...state,
        ...payload,
        isShowPublishCustomizeOrderModal: false,
        currentContractDetail: {}
      }
    },
    showDoDesignModal(state, { payload }) {
      return {
        ...state,
        ...payload,
        isShowDoDesignModal: true
      }
    },
    hideDoDesignModal(state, { payload }) {
      return {
        ...state,
        ...payload,
        isShowDoDesignModal: false,
        currentContractDetail: {}
      }
    },
    setListState(state, { payload }) {
      return {
        ...state,
        ...payload,
        listState: payload.listState
      }
    },
    setCurrentSystemProperty(state, { payload }) {
      return {
        ...state,
        ...payload,
        currentSystemProperty: payload.currentSystemProperty
      }
    },
    showPublishAgainCustomizeModal(state, { payload }) {
      return {
        ...state,
        ...payload,
        isShowPublishAgainCustomizeOrderModal: true
      }
    },
    hidePublishAgainCustomizeModal(state, { payload }) {
      return {
        ...state,
        ...payload,
        isShowPublishAgainCustomizeOrderModal: false,
        currentContractDetail: {}
      }
    },
    detailSuccess(state, { payload }) {
      return {
        ...state,
        ...payload
      }
    },
    showContractDetailModal(state, { payload }) {
      return {
        ...state,
        ...payload,
        isShowContractDetailModal: true
      }
    },
    hideContractDetailModal(state, { payload }) {
      return {
        ...state,
        ...payload,
        isShowContractDetailModal: false,
        currentContractDetail: {}
      }
    },
    setAddLoading(state, { payload }) {

      return {
        ...state,
        ...payload
      }
    },
    showMaterialModalVisible(state, { payload }) {
      state.material.modalVisible = true

      return {
        ...state
      }
    },
    hideMaterialModalVisible(state, { payload }) {
      state.material.modalVisible = false
      state.material.selectedKeys = []
      state.material.selectedItems = []
      state.material.items = []
      state.material.pagination = {
        current: 1,
        total: 0,
        showTotal: total => `共 ${total} 条`,
        showSizeChanger: true,
        showQuickJumper: true,
        defaultPageSize: defaultPageSize
      }

      return {
        ...state
      }
    },
    setMaterialSelected(state, { payload }) {
      state.material.selectedKeys = payload.keys
      state.material.selectedItems = [...new Set(state.material.selectedItems.concat(payload.items))].filter(item => state.material.selectedKeys.findIndex(element => element === item.id) > -1)

      return {
        ...state
      }
    },
    materialQuerySuccess(state, { payload }) {
      state.material.items = payload.content
      state.material.pagination = {
        ...state.material.pagination,
        current: payload.number + 1,
        total: payload.totalElements
      }
      return {
        ...state
      }
    },
    setMaterialTypeTree(state, { payload }) {
      state.material.typeTree = payload.filter(item => item.name !== '运输线路')

      return {
        ...state
      }
    },
    clearMaterialTypeTree(state, { payload }) {
      state.material.typeItem = {}

      return {
        ...state
      }
    },
    setPurchaseTypeToPayOrder(state, { payload }) {
      let commodity = {};
      commodity.id = payload.record.id
      commodity.purchaseType = payload.purchaseType;
      let _flag = false;
      for(let i=0, len=state.commodities.length; i<len; i++){
        if(state.commodities[i].id == commodity.id){
          state.commodities[i] = commodity;
          _flag = true;
        }
      }
      if(!_flag){
        state.commodities.push(commodity);
      }
      return {
        ...state
      }
    },
    initPurchaseType(state, { payload }) {
      state.commodities = []
      return {
        ...state
      }
    },

    setPayOrderList(state, { payload }) {
      state.payOrderList = payload
      return {
        ...state
      }
    },
    setSearchKeyword(state, { payload }) {
      const { keyword } = payload
      state.material.treeSearchKeyword = keyword

      return {
        ...state
      }
    },
    resetCurrentOrderDetail(state, { payload }) {
      return {
        ...state,
        currentOrderDetail: {}
      }
    },
    setTypeItem(state, { payload }) {
      state.material.typeItem = payload

      return {
        ...state
      }
    },
    setCurrentRefreshRecordId(state, { payload }) {
      return {
        ...state,
        currentRefreshRecordId: payload
      }
    },
    setIsShowPayOrderPushModal(state, { payload }) {
      return {
        ...state,
        isShowPayOrderPushModal: payload
      }
    },
    setCurrentPayOrder(state, { payload }) {
      return {
        ...state,
        currentPayOrder: payload
      }
    },
    asyncMaterialData(state, { payload }) {
      if (payload.isTablePagination) {
        delete payload.isTablePagination
        state.material.queryParam = {
          ...state.material.queryParam,
          ...payload
        }
        return {
          ...state
        }
      } else {
        state.material.queryParam = {
          ...payload
        }
        return {
          ...state
        }
      }
    }
  }
}
